<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
define('PUBLIC_PATH', str_replace('/index.php', '', \think\Request::instance()->root()));
define('INDEX_STATIC', "/public/static/index");
define('ADMIN_STATIC', "/public/static/admin");
define('P_STATIC', "/public/static");


/**
 * 模糊字符查询
 * @param $param
 * @return string
 */
function vague_str($param){
    $find = array('\\', '/', '%', '_', '&');
    $replace = array('\\\\', '\/', '\%', '\_', '\&');
    $param = '%' . trim(str_replace($find, $replace, $param)) . '%';
    return $param;
}

/**
 * sql查询时间区间
 * @param $delimiter
 * @param $param
 * @return array
 */
function sql_between_time($delimiter,$param){
    $time_arr = explode($delimiter, $param);
    return ['between time',$time_arr];
}


// 应用公共文件
/**
 *
 * @param type $code 10000表示为正确,其他为错误代码
 * @param type $message 提示消息
 * @param type $result 返回数据
 */
function my_json_encode($code, $message = "", $result = '')
{
    $data = array('code' => $code, 'msg' => $message);
    if ($result){
        $data['result']= $result;
    }
    if (!empty($_GET['callback'])) {
        echo $_GET['callback'] . '(' . json_encode($data) . ')';
    } else {
        echo json_encode($data);
    }
    exit;
}

function tree($data, $root = 0, $id = 'id', $parent = 'parent')
{
    $tree = [];
    foreach ($data as $row) {
        if ($row[$parent] == $root) {
            $tmp = tree($data, $row[$id], $id, $parent);
            if ($tmp) {
                $row['children'] = $tmp;
            }
            $tree[] = $row;
        }
    }
    return $tree;
}
